<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$cashierid=$this->checklogin();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('cashierid'=>$cashierid,'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_cashier_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	
	$return['status']=array('0'=>'下架','1'=>'上架');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'商品管理'),
			array('op'=>'edit','title'=>'添加/编辑'),
			
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
								
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'totle','title'=>'库存','type'=>'string'),
			
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),								
			
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
			
		),
	
		),
		'search'=>array(
			
			array('name'=>'title','width'=>15,'title'=>'名称','type'=>'string'),
			
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_cashier_goods',array('id'=>$id));

	}else{
		$item=array();
	}
	$return['status']=array('0'=>'下架','1'=>'上架');
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		
		'edit'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),				
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'total','title'=>'库存','type'=>'string'),
			array('name'=>'categoryid','title'=>'选择商品分类','type'=>'select','options'=>'category','option_title'=>'catename'),
			array('name'=>'goodssn','title'=>'条码','type'=>'string'),								
			
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			)
			);
	
	
	
	$return['category']=pdo_getall('jy_cashier_goods_category',array('uniacid'=>$_SESSION['uniacid']),array('id','catename'));
	
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='post'){
	$post=get_postdata();
	$data=array_fields(array('title','image','price','total','categoryid','goodssn','status'),$post);
	
	if($id){
				pdo_update('jy_cashier_goods',$data,array('id'=>$id));
				exi('设置成功！','','/list/');
			}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				$data['cashierid']=$cashierid;
				pdo_insert('jy_cashier_goods',$data);
				exi('添加成功！','','/list/');
			}
	
	exi('设置成功','','list');
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_cashier_goods',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_cashier_goods',array('id'=>$id));	
			}
		}
	}
	exi('删除成功！');
}